<?php


use App\Library\Utils\Arr;
use Phinx\Seed\AbstractSeed;
use Faker\Factory as FakerFactory;

class ImUserSeeder extends AbstractSeed
{
    public function getDependencies(): array
    {
        return [
            'UserSeeder',
        ];
    }

    /**
     * Run Method.
     *
     * Write your database seeder using this method.
     *
     * More information on writing seeders is available here:
     * https://book.cakephp.org/phinx/0/en/seeding.html
     */
    public function run()
    {
        $faker = FakerFactory::create('zh_CN');

        $users = $this->fetchAll('SELECT * FROM kg_user');
        $imUsers = $this->fetchAll('SELECT * FROM kg_im_user');
        $iuids = array_map(function ($user) {
            return $user['id'];
        }, $imUsers);

        $rows = [];
        foreach ($users as $user) {
            if (in_array($user['id'], $iuids)) {
                continue;
            }
            $rows[] = [
                'id' => $user['id'],
                'name' => $user['name'],
                'avatar' => $user['avatar'],
                'sign' => '',
                'skin' => '',
                'status' => Arr::rand(['hide', 'online']),
                'friend_count' => rand(0, 50),
                'group_count' => rand(0, 30),
                'create_time' => $faker->unixTime,
                'update_time' => $faker->unixTime,
            ];
        }
        $this->table('kg_im_user')->insert($rows)->saveData();
    }
}
